package com.android.delivery.activity.wangdian

import android.os.Bundle
import android.view.View
import android.widget.FrameLayout
import androidx.recyclerview.widget.RecyclerView
import com.android.delivery.R
import com.android.delivery.adapter.siji.OrderAdapter
import com.android.delivery.api.ApiService
import com.android.delivery.base.BaseListFragmet
import com.android.delivery.callback.DialogCallback
import com.android.delivery.callback.LzyResponse
import com.android.delivery.constant.Constants
import com.android.delivery.entity.Logistics
import com.android.delivery.util.DensityUtil
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.include_refresh_layout_list.*

/**
 * @author: mqwang
 * @create: 3/18/21 8:35 AM
 * @Description:骑手端-历史订单
 */
class RiderHistoryOrderListFragment : BaseListFragmet<Logistics>() {

    companion object {
        fun newInstance(type: Int): RiderHistoryOrderListFragment {
            val fragment = RiderHistoryOrderListFragment()
            val bundle = Bundle()
            bundle.putInt(Constants.TYPE, type)
            fragment.arguments = bundle
            return fragment
        }
    }

    private val orderAdapter: OrderAdapter by lazy {
        OrderAdapter()
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        recyclerView.setBackgroundResource(R.drawable.bg_common)
        val margin = DensityUtil.dp2px(12f);
        (recyclerView.layoutParams as FrameLayout.LayoutParams).setMargins(margin, margin, margin, margin);
    }

    override fun bindRecyclerView(recLv: RecyclerView?) {
        orderAdapter.setType(1)
        recLv!!.adapter = orderAdapter
        orderAdapter.setOnItemClickListener { adapter, view, position ->

        }
    }

    override fun loadData(page: Int, pageCount: Int) {
        val type = arguments!!.getInt(Constants.TYPE)
        if (type == 0) {
            ApiService.deliveryList(this, 1, page, object : DialogCallback<LzyResponse<Logistics>>() {
                override fun onSuccess(response: Response<LzyResponse<Logistics>>?) {
                    onGetDataSuc(response!!.body().data.records)
                }
            })
        } else {
            ApiService.packHistoryList(this, page, object : DialogCallback<LzyResponse<Logistics>>() {
                override fun onSuccess(response: Response<LzyResponse<Logistics>>?) {
                    onGetDataSuc(response!!.body().data.records)
                }
            })
        }

    }

    override fun getData(): MutableList<Logistics> {
        return orderAdapter.data
    }

    override fun bindData(isRefresh: Boolean, list: MutableList<Logistics>?) {
        if (isRefresh) {
            orderAdapter.setNewData(list)
        } else {
            orderAdapter.addData(list!!)
        }
    }
}